﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TrainingWorkshop.Model;
using System.Data;
using System.Data.SqlClient;

namespace TrainingWorkshop.NewDAL
{
    public class DepartmentRepository:IDepartmentRepository
	{
        private SqlConnection sqlconn;

        public DepartmentRepository(SqlConnection TConn)
        {
            sqlconn = TConn;
           //DepartmentInstance.DepartmentId=Department.DepartmentId;
           //DepartmentInstance.DepartmentName=Department.DepartmentName;
           //DepartmentInstance.Manager=Department.Manager;
           //DepartmentInstance.Phone=Department.Phone;
           //DepartmentInstance.Remark=Department.Remark;


        }

        public List<Department> Select(Department DepartmentInstance)
        {
            DataSet ds = new DataSet();
            List<Department> result = new List<Department>();
            string str = "select * from Department where DepartmentId=@DepartId";

            SqlParameter par = new SqlParameter("@DepartId", DepartmentInstance.DepartmentId);
            SqlCommand cmd = new SqlCommand(str, sqlconn);
            cmd.Parameters.Add(par);

            try
            {
                sqlconn.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(ds, "Result");
                
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    Department a = new Department(Convert.ToInt32(row[0]), row[1].ToString().Trim(), row[2].ToString().Trim(), row[3].ToString().Trim(), row[4].ToString().Trim());
                    result.Add(a);
                }

            }
            catch (Exception e)
            {
               
                
            }

            finally
            {
                sqlconn.Close();
            }

            return result;

        }

        public List<Department> SelectAll()
        {
             DataSet ds = new DataSet();
            List<Department> result = new List<Department>();
            string str = "select * from Department";

            SqlCommand cmd = new SqlCommand(str, sqlconn);
           

            try
            {
                sqlconn.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(ds, "Result");
                
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    Department a = new Department(Convert.ToInt32(row[0]), row[1].ToString().Trim(), row[2].ToString().Trim(), row[3].ToString().Trim(), row[4].ToString().Trim());
                    result.Add(a);
                }

            }
            catch (Exception e)
            {
               
                
            }

            finally
            {
                sqlconn.Close();
            }

            return result;
        }

        public void Update(Department DepartmentInstance)
        {
            throw new NotImplementedException();
        }

        public void Create(Department DepartmentInstance)
        {
            throw new NotImplementedException();
        }

        public void Delete(Department DepartmentInstance)
        {
            throw new NotImplementedException();
        }
    }
}
